'use strict';

require('sugar');
var fs = require('fs-extra');
var winston = require('winston');

// Default logger options
var levels = {
  error: 4,
  warn: 3,
  info: 2,
  debug: 1,
  trace: 0
};

var colors = {
  error: 'red',
  warn: 'orange',
  info: 'green',
  debug: 'blue',
  trace: 'yellow'
};

var console = {
  level: 'info',
  json: true,
  handleExceptions: true,
  colorize: true,
  prettyPrint: true,
  timestamp: function () {
    return Date.create().format('{dd} {Mon} {yyyy} {hh}:{mm}:{ss},{fff}');
  }
};

var file = {
  level: 'info',
  json: true,
  filename: './msvc.log',
  handleExceptions: true,
  maxsize: 10485760,
  maxFile: 10,
  timestamp: function () {
    return Date.create().format('{dd} {Mon} {yyyy} {hh}:{mm}:{ss},{fff}');
  }
};

module.exports = exports = function (opts) {
  opts = opts || {};

  Object.merge(levels, opts.levels || {});
  Object.merge(colors, opts.colors || {});

  var transports = [];

  if (opts.console && opts.console.enabled) {
    Object.merge(console, Object.reject(opts.console, 'enabled'));
    transports.add(new(winston.transports.Console)(console));
  }

  if (opts.file && opts.file.enabled) {
    fs.createFileSync(opts.file.filename);
    Object.merge(file, Object.reject(opts.file, 'enabled'));
    transports.add(new(winston.transports.File)(file));
  }

  winston.addColors(colors);
  return new(winston.Logger)({
    levels: levels,
    transports: transports,
    exitOnError: false
  });
};